Page({
  data: {
    userInfo: {},
    activeTab: 'all',
    myWork: [
      {
        id: 1,
        projectId: 1,
        projectName: '生活轨迹系统开发',
        moduleName: '前端开发',
        title: '首页UI设计',
        description: '设计应用的首页界面，包括布局、配色和交互元素。需要保持整体风格一致，采用蓝色为主色调，并且适配不同尺寸的手机屏幕。需要包含用户信息展示、统计卡片和快捷操作按钮等元素。',
        priority: 'high',
        status: 'in-progress',
        startDate: '2024-03-10',
        deadline: '2024-03-25',
        progress: 60
      },
      {
        id: 2,
        projectId: 1,
        projectName: '生活轨迹系统开发',
        moduleName: '前端开发',
        title: '用户注册登录功能',
        description: '实现用户的注册、登录和找回密码功能。需要包含表单验证，密码强度检测，以及登录状态保持功能。同时需要对接后端API，实现数据的存储和验证。',
        priority: 'medium',
        status: 'pending',
        startDate: '2024-03-20',
        deadline: '2024-04-05',
        progress: 0
      },
      {
        id: 3,
        projectId: 2,
        projectName: '数据分析平台',
        moduleName: '数据采集',
        title: '数据源接口对接',
        description: '对接多个数据源API，实现数据的自动采集。需要支持REST和GraphQL接口，并设计适配器模式来处理不同格式的数据源。配置定时任务实现数据的定期更新和同步。',
        priority: 'high',
        status: 'completed',
        startDate: '2024-03-05',
        deadline: '2024-03-15',
        progress: 100
      }
    ],
    filteredWork: []
  },

  onLoad() {
    // 获取用户信息
    const app = getApp();
    this.setData({
      userInfo: app.globalData.userInfo || {}
    });
    
    // 初始过滤所有工作
    this.filterWork('all');
  },

  // 切换标签页
  changeWorkTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({ activeTab: tab });
    this.filterWork(tab);
  },

  // 根据标签过滤工作
  filterWork(tab) {
    let filtered;
    
    switch(tab) {
      case 'pending':
        filtered = this.data.myWork.filter(item => item.status === 'pending');
        break;
      case 'in-progress':
        filtered = this.data.myWork.filter(item => item.status === 'in-progress');
        break;
      case 'completed':
        filtered = this.data.myWork.filter(item => item.status === 'completed');
        break;
      default:
        filtered = this.data.myWork;
    }
    
    this.setData({ filteredWork: filtered });
  },

  // 更新工作进度
  updateProgress(e) {
    const id = parseInt(e.currentTarget.dataset.id);
    const value = e.detail.value;
    
    const myWork = this.data.myWork.map(item => {
      if (item.id === id) {
        // 如果进度为100%，自动设置状态为已完成
        const status = value === 100 ? 'completed' : (value > 0 ? 'in-progress' : item.status);
        return { ...item, progress: value, status: status };
      }
      return item;
    });
    
    this.setData({ myWork }, () => {
      this.filterWork(this.data.activeTab);
    });
    
    wx.showToast({
      title: '进度已更新',
      icon: 'success'
    });
  },

  // 更新工作状态
  updateStatus(e) {
    const id = parseInt(e.currentTarget.dataset.id);
    const status = e.currentTarget.dataset.status;
    
    const myWork = this.data.myWork.map(item => {
      if (item.id === id) {
        // 如果设置为已完成，自动设置进度为100%
        const progress = status === 'completed' ? 100 : item.progress;
        return { ...item, status: status, progress: progress };
      }
      return item;
    });
    
    this.setData({ myWork }, () => {
      this.filterWork(this.data.activeTab);
    });
    
    wx.showToast({
      title: '状态已更新',
      icon: 'success'
    });
  },

  // 查看项目详情
  viewProjectDetail(e) {
    const projectId = e.currentTarget.dataset.id;
    
    wx.navigateTo({
      url: `/pages/projects/projects?id=${projectId}`
    });
  },

  // 判断是否临近截止日期（7天内）
  isNearDeadline(dateStr) {
    const today = new Date();
    const deadline = new Date(dateStr);
    const diffTime = deadline - today;
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
    return diffDays <= 7 && diffDays >= 0;
  },

  // 获取状态文字
  getStatusText(status) {
    switch(status) {
      case 'pending': return '待处理';
      case 'in-progress': return '进行中';
      case 'completed': return '已完成';
      default: return '未知';
    }
  },

  // 获取状态样式类
  getStatusClass(status) {
    switch(status) {
      case 'pending': return 'status-pending';
      case 'in-progress': return 'status-in-progress';
      case 'completed': return 'status-completed';
      default: return '';
    }
  }
}) 